package com.mouliu.generate1;

import com.lmax.disruptor.EventTranslator;
import com.lmax.disruptor.dsl.Disruptor;
import com.mouliu.generate.Trade;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * @author mouliu
 * @create 2018-06-08-下午11:45
 */
public class TradePublisher implements Runnable{
    Disruptor<Trade>disruptor;
    private CountDownLatch latch;

    private static int LOOP = 10;//模拟百万次交易的发生

    public TradePublisher(CountDownLatch latch,Disruptor<Trade>disruptor){
        this.disruptor = disruptor;
        this.latch = latch;
    }
    @Override
    public void run() {
        TradeEventTranslator tradeEventTranslator = new TradeEventTranslator();
        for (int i=0;i<LOOP;i++){
            disruptor.publishEvent(tradeEventTranslator);
        }
        latch.countDown();
    }
}
class TradeEventTranslator implements EventTranslator<Trade>{
    private Random random = new Random();
    @Override
    public void translateTo(Trade trade, long l) {
        this.generateTrade(trade);
    }
    private Trade generateTrade(Trade trade){
        trade.setPrice(random.nextDouble()*9999);
        return trade;
    }
}
